{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def EuDis(x1,y1,x2,y2):\n",
    "    return math.sqrt((x1-x2)**2 + (y1-y2)**2)\n",
    "\n",
    "def EuDisDot(dot1,dot2):\n",
    "    return EuDis(dot1[0],dot1[1], dot2[0],dot2[1])\n",
    "\n",
    "def cal_Center(arr):\n",
    "    x = 0\n",
    "    y=0\n",
    "    for dot in arr:\n",
    "        x+=dot[0]\n",
    "        y+=dot[1]\n",
    "    \n",
    "    return (x/len(arr), y/len(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "dots=[(5.9, 3.2),\n",
    " (4.6, 2.9),\n",
    " (6.2, 2.8),\n",
    " (4.7, 3.2),\n",
    " (5.5, 4.2),\n",
    " (5.0, 3.0),\n",
    " (4.9, 3.1),\n",
    " (6.7, 3.1),\n",
    " (5.1, 3.8),\n",
    " (6.0, 3.0)]\n",
    "\n",
    "red=(6.2,3.2)\n",
    "green = (6.6,3.7)\n",
    "blue=(6.5,3.0)\n",
    "\n",
    "reds = []\n",
    "greens = []\n",
    "blues=[]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "for i in range(2):\n",
    "    reds = []\n",
    "    greens = []\n",
    "    blues=[]\n",
    "    for dot in dots :\n",
    "        dr = EuDisDot(dot, red)\n",
    "        dg = EuDisDot(dot, green)\n",
    "        db = EuDisDot(dot, blue)\n",
    "        if dr == min(dr,dg,db):\n",
    "            reds.append(dot)\n",
    "        if dg == min(dr,dg,db):\n",
    "            greens.append(dot)\n",
    "        if db == min(dr,dg,db):\n",
    "            blues.append(dot)\n",
    "    \n",
    "    red = cal_Center(reds)\n",
    "    green = cal_Center(greens)\n",
    "    blue = cal_Center(blues)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6.05, 3.9500000000000006)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cal_Center(greens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(6.05, 3.95), (5.5, 4.2), (6.6, 3.7)]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "greens"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
